package org.jeecg.modules.jiucxldj.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.event.ActivitiEntityEvent;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.jeecg.modules.jiucxldj.entity.AgJiucxldj;
import org.jeecg.modules.jiucxldj.service.IAgJiucxldjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component("jiucxldjTaskEvent")
public class EventListener implements ActivitiEventListener {
    @Value(value = "${santy.apiServer}")
    String apiServer;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private IAgJiucxldjService agJiucxldjService;

    public void onEvent(ActivitiEvent event) {
        Map<String, Object> params = null;
        TaskEntity taskEntity = (TaskEntity)((ActivitiEntityEvent)event).getEntity();
        ActivitiEventType type = event.getType();
        log.info("eventType", type);

        if (type == ActivitiEventType.TASK_ASSIGNED) {
            params = new HashMap<>();

            params.put("systemCode", "ZHAG");
            String processInstanceId = taskEntity.getProcessInstance().getId();
            params.put("processCode", processInstanceId);
            params.put("createTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

            AgJiucxldj hqYlgl = agJiucxldjService.getOne(new LambdaQueryWrapper <AgJiucxldj>().eq(AgJiucxldj::getProcessInstanceId, processInstanceId));
            if(hqYlgl != null)
                params.put("initiateUser", hqYlgl.getCreateBy());

            params.put("title", "工作检查登记");
//            params.put("detailLink", "/#/jiucxldjdb");
            Object status = taskEntity.getVariable("status");
//            String status = status.toString();
            if (status == null || status.equals("1")) {
                params.put("detailLink", "/#/problemdb");

            }else{
                if(status.toString().equals("0")){
                    params.put("detailLink", "/#/jiucxldjsq");
                }
            }
            params.put("approverUsers", taskEntity.getAssignee());
            params.put("describe", "");
            params.put("listLink", "");
            params.put("initiateListLink", "");
            params.put("initiateDetailLink", "/#/jiucxldjsq");

            log.info("TASK_ASSIGNED, 准备启动代办线程");
            new Thread(new SantydbThread(log,apiServer + "/api/jsonws/ToDoList-portlet.approvaltodo/addApprovalToDos?jsonParam={jsonParam}",
                    restTemplate, params), "进入任务结点").start();
            log.info("TASK_ASSIGNED, 启动线程成功");
        }

        if (type == ActivitiEventType.TASK_COMPLETED) {
            params = new HashMap<>();

            params.put("systemCode", "ZHAG");
            String processInstanceId = taskEntity.getProcessInstance().getId();
            params.put("processCode", processInstanceId);

            params.put("disposeTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

            AgJiucxldj agJiucxldj = agJiucxldjService.getOne(new LambdaQueryWrapper<AgJiucxldj>().eq(AgJiucxldj::getProcessInstanceId, processInstanceId));
            if(agJiucxldj != null)
                params.put("initiateUser", agJiucxldj.getCreateBy());

            params.put("detailLink", "/#/problemxq");

            params.put("lastApproverUser", taskEntity.getAssignee());
            log.info("TASK_COMPLETED, 准备启动代办线程");
            new Thread(new SantydbThread(log,apiServer + "/api/jsonws/ToDoList-portlet.approvaltodo/addToDoEnd?jsonParam={json}",
                    restTemplate, params), "离开任务结点").start();

            log.info("TASK_COMPLETED, 启动线程成功");
        }


    }

    public boolean isFailOnException(){
        return true;
    }
}
